let timer = null;
self.onmessage = function (e) {
  if (e.data.command == 'start') {
    timer = setInterval(() => {
      fetch(e.data.url, {
        method: 'POST',
        headers: {
          'Authorization': e.data.token,
          'Content-Type': 'application/json'
        },
        body: e.data.body,
        credentials: 'include'
      }).then(response => {
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return response.json();
      }).then(data => {
        self.postMessage({
          type: 'newMessage',
          data: data
        });
      }).catch(error => {
        self.postMessage({
          type: 'error',
          error: error.message
        });
      });
    }, e.data.delay);
  } else if (e.data.command == 'stop') {
    clearInterval(timer);
  } else {
  }
};
